package cn.hdu.liang.ch5;

import cn.hdu.liang.ch4.Dish;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

import static cn.hdu.liang.ch4.Dish.menu;

public class Mapping {
    public static void main(String[] args) {
        List<String> dishNames = menu.stream().map(Dish::getName).collect(Collectors.toList());
        System.out.println(dishNames);

        List<String> words = Arrays.asList("Java 8", "Lambdas", "In", "Action");
        List<Integer> wordsLengths = words.stream()
                .map(String::length)
                .collect(Collectors.toList());
        System.out.println(wordsLengths);

        List<Integer> dishLengths = menu.stream()
                .map(Dish::getName)
                .map(String::length)
                .collect(Collectors.toList());
        System.out.println(dishLengths);

        //流的扁平化
        List<String> uniqueChars = words.stream()
                .map(w -> w.split(""))
                .flatMap(Arrays::stream)
                .distinct()
                .collect(Collectors.toList());
        System.out.println(uniqueChars);

        List<Integer> ints = Arrays.asList(1, 2, 3, 4, 5);
        List<Integer> sqInts = ints.stream().map(i -> i * i).collect(Collectors.toList());
        System.out.println(sqInts);

        List<Integer> num1 = Arrays.asList(1, 2, 3);
        List<Integer> num2 = Arrays.asList(3, 4);
        List<int[]> intResults = num1.stream()
                .flatMap(i -> num2.stream()
                        .map(j -> new int[]{i, j}))
                .collect(Collectors.toList());
        System.out.println(intResults.stream().map(Arrays::toString).collect(Collectors.toList()));

        List<int[]> pairs = num1.stream()
                .flatMap(i -> num2.stream()
                        .filter(j -> (i + j) % 3 == 0)
                        .map(j -> new int[]{i, j}))
                .collect(Collectors.toList());
        System.out.println(pairs.stream().map(Arrays::toString).collect(Collectors.toList()));
    }
}
